//
//  KLRBSettingTableViewController.m
//  KISSLOCK
//
//  Created by 华杨科技 on 2017/4/11.
//  Copyright © 2017年 陈宾鹏. All rights reserved.
//

#import "KLRBSettingTableViewController.h"
#import "KLHelp.h"
#import "HemaConst.h"
#import "KLInstructionsViewController.h"
#import "UMFeedback.h"
#import "KLAboutViewController.h"
#import "JRToast.h"

#import "KLRealNameCertifyViewController.h"
#import "KLDataStrongboxViewController.h" // 数据保险箱密码
@interface KLRBSettingTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIViewController *umFeedBackVC;

@end

typedef enum : NSUInteger {
    SettingInstructions = 0, // 使用说明
    SettingAbout, // 关于
    SettingReset, // 重置数据保险箱密码
    SettingRecover, // 恢复数据
    SettingFeedBack, // 意见反馈
    SettingPolicy, // 相关政策
    SettingRealNameCertification, // 实名认证
    SettingLoginOut, // 退出登录
} SettingType;

static NSString *cellIdentifier =@"cellIdentifier";

@implementation KLRBSettingTableViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化导航条
    [self navSet];
    
    _dataArray = @[@"使用说明",@"关于",@"重设数据保险箱密码",@"恢复数据",@"意见反馈",@"相关政策",@"实名认证",@"退出登录"].mutableCopy;

    // 注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
}

- (void)navSet {
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = RGB_UI_COLOR(242, 242, 242);
    self.title = @"设置";
    
    self.umFeedBackVC = [UMFeedback feedbackModalViewController];
    
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:(UIControlEventTouchUpInside)];
    }];
} - (void)doBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    // 防止cell重复问题
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }else {
        //删除cell的所有子视图
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // 取消点击样式
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // >
    cell.textLabel.text = self.dataArray[indexPath.section];
    
    if (indexPath.section == 7) {
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor redColor];
        cell.textLabel.font = [UIFont systemFontOfSize:18];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case SettingInstructions:
        { KLInstructionsViewController *introductionVC = [[KLInstructionsViewController alloc] init];
            introductionVC.whichWeb = 2;
            [self.navigationController pushViewController:introductionVC animated:YES];
        }break;
            
        case SettingAbout:{
            KLAboutViewController *aboutVC = [[KLAboutViewController alloc]init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }break;
        
        case SettingReset:{
            // 登录请求数据时把是否设置了数据保险箱 保存到本地，
            // 这里需要做一个判断,是:就跳转到重设数据宝箱界面 否:跳转设置数据保险箱密码
            KLDataStrongboxViewController *strongboxVC = [[KLDataStrongboxViewController alloc] init];
            strongboxVC.title = @"设置数据保险箱密码";
            [self.navigationController pushViewController:strongboxVC animated:YES];
        }break;
            
        case SettingRecover:{
            [JRToast showWithText:@"暂未开通" duration:1.0];
        }break;
        
        case SettingFeedBack:{
            [self.navigationController presentViewController:self.umFeedBackVC animated:YES completion:nil];
        }break;
            
        case SettingPolicy:{
            KLInstructionsViewController *instructionVC = [[KLInstructionsViewController alloc]init];
            instructionVC.whichWeb = 3;
            [self.navigationController pushViewController:instructionVC animated:YES];
        }break;
        
        case SettingRealNameCertification:{
            KLRealNameCertifyViewController *rnVC = [[KLRealNameCertifyViewController alloc] init];
            [self.navigationController pushViewController:rnVC animated:YES];
        }break;
            
        case SettingLoginOut:{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginOut" object:nil userInfo:nil];
            [self.navigationController popViewControllerAnimated:YES]; // 每次登录之后回到主界面
        }break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_View_Width, 7)];
    headerView.backgroundColor = RGB_UI_COLOR(242, 242, 242);
    return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_View_Width, 7)];
        footView.backgroundColor = RGB_UI_COLOR(242, 242, 242);
        return footView;
}

@end
